package nc.ui.am.common.utils;

import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

public class GUIUtils {

	public static void processOnSwingEventThread(Runnable todo) {
		processOnSwingEventThread(todo, false);
	}

	public static void processOnSwingEventThread(Runnable todo, boolean wait) {
		if (todo == null) {
			throw new IllegalArgumentException("Runnable == null");
		}

		if (wait) {
			if (SwingUtilities.isEventDispatchThread()) {
				todo.run();
			} else {
				try {
					SwingUtilities.invokeAndWait(todo);
				} catch (InvocationTargetException ex) {
					throw new RuntimeException(ex);
				} catch (InterruptedException ex) {
					throw new RuntimeException(ex);
				}
			}
		} else {
			if (SwingUtilities.isEventDispatchThread()) {
				todo.run();
			} else {
				SwingUtilities.invokeLater(todo);
			}
		}
	}

}
